package com.lions.client.view.weather;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.lions.client.view.popup.MyPopupPanel;

// TODO: Auto-generated Javadoc
/**
 * The Class WeatherPanel.
 */
public class WeatherPanel extends AbsolutePanel {

	/**
	 * Instantiates a new weather panel.
	 */
	public WeatherPanel() {
		updateWeather();

		// This is the style that will place the logo in the bottom right corner
		// of the weather panel.

		addStyleName("weatherBack");
	}

	/*
	 * This is the function that will be called to create the information for
	 * the weather.
	 * 
	 * This is done using JSONP or JSON with padding, and uses the
	 * requestbuilder to get the data, i did it this way because I didnt want to
	 * set up a proxy server to retrieve the data.
	 * 
	 * Parameter: None Author: Kyle Fink Information about this function can be
	 * found on the professors tech talks about weather
	 */

	private void updateWeather() {
		String url = "http://api.wunderground.com/api/5cf41ff0b9404421/conditions/q/55812.json?";
		String url2 = "http://api.wunderground.com/api/5cf41ff0b9404421/astronomy/q/55812.json?";
		final VerticalPanel vp = new VerticalPanel();

		final JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
		jsonp.setCallbackParam(this.callBack);
		jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>() {
			@Override
			public void onFailure(final Throwable caught) {
				new MyPopupPanel(JSONFailure, JSONonFailure);
			}

			@Override
			public void onSuccess(final JavaScriptObject s) {
				String temp = null;
				String vis = null;
				String wind = null;
				String show = null;
				JSONObject obj = new JSONObject(s);

				JSONValue current = obj.get("current_observation");
				JSONObject jB = (JSONObject) JSONParser.parseLenient(current
						.toString());
				JSONValue tempC = jB.get("temp_c");
				JSONValue visKL = jB.get("visibility_km");
				JSONValue windKPH = jB.get("wind_kph");
				JSONValue icon = jB.get("icon_url");
				if (tempC == null) {
					Window.alert("KEY IS NULL");
				} else {
					show = icon.toString();
					temp = tempC.toString();
					vis = visKL.toString();
					wind = windKPH.toString();
				}

				final Image image = new Image();
				image.setUrl(show);

				Label tempLabel = new Label("The Current Temp is: " + temp
						+ " C");
				Label visLabel = new Label("The Current Visability is: " + vis
						+ " km");
				Label windLabel = new Label("The Current Wind Speed is: "
						+ wind + " KPH");

				tempLabel.addStyleName(weatherText);
				visLabel.addStyleName(weatherText);
				windLabel.addStyleName(weatherText);

				vp.add(tempLabel);
				vp.add(visLabel);
				vp.add(windLabel);
				vp.setStyleName("weatherpanel");
				add(vp);
			}
		});

		final JsonpRequestBuilder jsonp2 = new JsonpRequestBuilder();
		jsonp2.setCallbackParam(this.callBack);
		jsonp2.requestObject(url2, new AsyncCallback<JavaScriptObject>() {

			@Override
			public void onFailure(final Throwable caught) {
				new MyPopupPanel(JSONFailure, JSONonFailure);

			}

			@Override
			public void onSuccess(final JavaScriptObject result) {
				String sunDownHour = null;
				String sunDownMin = null;
				JSONObject obj2 = new JSONObject(result);

				JSONValue astro = obj2.get("moon_phase");
				JSONObject jO = (JSONObject) JSONParser.parseLenient(astro
						.toString());
				JSONValue Hour = jO.get("sunset");

				if (Hour != null) {
					// int nine = 9;
					int eleven = 11;
					int twentyfour = 24;
					int twentysix = 26;
					sunDownHour = Hour.toString().substring(nine, eleven);
					sunDownMin = Hour.toString().substring(twentyfour,
							twentysix);
				} else {
					Window.alert("JSONValue is null");
				}

				Label sunDown = new Label("Sundown is at: " + sunDownHour + ":"
						+ sunDownMin);

				sunDown.addStyleName(weatherText);
				vp.add(sunDown);

			}

		});

	}

	/** The nine. */
	private int nine = 9;

	/** The weather text. */
	private String weatherText = "weatherText";

	/** The JSON failure. */
	private String JSONFailure = "JSON Failure";

	/** The JSO non failure. */
	private String JSONonFailure = "JSONP onFailure";

	/** The call back. */
	private String callBack = "callback";
}
